<?php 

add_action('wp_ajax_nopriv_ajax_comment', 'ajax_comment_callback');
add_action('wp_ajax_ajax_comment', 'ajax_comment_callback');
function ajax_comment_callback(){
    global $wpdb;
    $comment_post_ID = isset($_POST['comment_post_ID']) ? (int) $_POST['comment_post_ID'] : 0;
    $post = get_post($comment_post_ID);
    $post_author = $post->post_author;
    if ( empty($post->comment_status) ) {
        do_action('comment_id_not_found', $comment_post_ID);
        ajax_comment_err('Invalid comment status.');
    }
    $status = get_post_status($post);
    $status_obj = get_post_status_object($status);
    if ( !comments_open($comment_post_ID) ) {
        do_action('comment_closed', $comment_post_ID);
        ajax_comment_err('Sorry, comments are closed for this item.');
    } elseif ( 'trash' == $status ) {
        do_action('comment_on_trash', $comment_post_ID);
        ajax_comment_err('Invalid comment status.');
    } elseif ( !$status_obj->public && !$status_obj->private ) {
        do_action('comment_on_draft', $comment_post_ID);
        ajax_comment_err('Invalid comment status.');
    } elseif ( post_password_required($comment_post_ID) ) {
        do_action('comment_on_password_protected', $comment_post_ID);
        ajax_comment_err('Password Protected');
    } else {
        do_action('pre_comment_on_post', $comment_post_ID);
    }
    $comment_author       = ( isset($_POST['author']) )  ? trim(strip_tags($_POST['author'])) : null;
    $comment_author_email = ( isset($_POST['email']) )   ? trim($_POST['email']) : null;
    $comment_content      = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
    $user = wp_get_current_user();
    if ( $user->exists() ) {
        if ( empty( $user->display_name ) )
            $user->display_name=$user->user_login;
        $comment_author       = esc_sql($user->display_name);
        $comment_author_email = esc_sql($user->user_email);
        $user_ID              = esc_sql($user->ID);
        if ( current_user_can('unfiltered_html') ) {
            if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) {
                kses_remove_filters();
                kses_init_filters();
            }
        }
    } else {
        if ( get_option('comment_registration') || 'private' == $status )
            ajax_comment_err('Sorry, you must be logged in to post a comment.');
    }
    $comment_type = '';
    if ( get_option('require_name_email') && !$user->exists() ) {
        if ( 6 > strlen($comment_author_email) || '' == $comment_author )
            ajax_comment_err( 'Error: please fill the required fields (name, email).' );
        elseif ( !is_email($comment_author_email))
            ajax_comment_err( 'Error: please enter a valid email address.' );
    }
    if ( '' == $comment_content )
        ajax_comment_err( 'Error: please type a comment.' );
    $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
    if ( $comment_author_email ) $dupe .= "OR comment_author_email = '$comment_author_email' ";
    $dupe .= ") AND comment_content = '$comment_content' LIMIT -1";
    if ( $wpdb->get_var($dupe) ) {
        return true;
    }
	
    if ( $lasttime = $wpdb->get_var( $wpdb->prepare("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author = %s ORDER BY comment_date DESC LIMIT 1", $comment_author) ) ) {
        $time_lastcomment = mysql2date('U', $lasttime, false);
        $time_newcomment  = mysql2date('U', current_time('mysql', 1), false);
        $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
        if ( $flood_die ) {
            ajax_comment_err('You are posting comments too quickly.  Slow down.');
        }
    }
    $comment_parent = isset($_POST['comment_parent']) ? absint($_POST['comment_parent']) : 0;
    $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_title', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');

    $comment_id = wp_new_comment( $commentdata );


    $comment = get_comment($comment_id);
    do_action('set_comment_cookies', $comment, $user);
    $comment_depth = 1;
    $tmp_c = $comment;
    while($tmp_c->comment_parent != 0){
        $comment_depth++;
        $tmp_c = get_comment($tmp_c->comment_parent);
    }
    $GLOBALS['comment'] = $comment;
    //这里修改成你的评论结构
    ?>
<li class="comment modal-content-hack <?php echo get_comment_meta($comment->comment_ID,'option',true); ?>" id="li-comment-<?php comment_ID(); ?>">
<div class="comments-title" role="tab" id="<?php echo '#heading'.get_comment_ID(); ?>">
  <a data-toggle="collapse" data-parent="#accordion" href="<?php echo '#comment-single-main'.get_comment_ID(); ?>" aria-controls="<?php echo 'comment-single-main'.get_comment_ID(); ?>">
	<span class="comment-single-sort">
		<?php  
			$outopt = get_comment_meta($comment->comment_ID,'option',true);
			if($outopt == 'download') {
				echo '[资源分享]';
			}else if($outopt == 'evaluation') {
				echo '[游戏测评]';
			}else {
				echo '[问题互动]';
			}
		?>
	</span>
	<span class="comment-single-title">
		<?php echo get_comment_meta($comment->comment_ID,'title',true); ?>
	</span>
	<span class="reply-count right">
	回复数量:<span class="reply-count-num"></span>
	</span>
  </a>
</div>
<div class="comments-main collapse" id="<?php echo 'comment-single-main'.get_comment_ID(); ?>" aria-labelledby="<?php echo 'heading'.get_comment_ID(); ?>" role="tabpanel" aria-expanded="false">
	<div class="comment-author">
		<div class="comment-author-avatar">
			<?php echo get_avatar(disnametoid(get_comment_author()), '72', '','img-thumbnail'); ?>
		</div>

		<div class="comment-author-info">
		
		</div>
	</div>   
	<div class="comment-content" id="comment-<?php comment_ID(); ?>">
	  <div class="comment-content-body">
		<div class="comment-author-name">			
			<?php echo get_comment_author_link(); ?>
			<?php 
				if(disnametoid(get_comment_author()) == NULL) {
					echo '游客';
				}
				else if(disnametoid(get_comment_author()) == get_post()->post_author) {
					if(get_the_author_meta('user_level', disnametoid(get_comment_author())) == 10) {
						echo '(作者，站长)';
					}
					else if(get_the_author_meta('user_level', disnametoid(get_comment_author())) == 7) {
						echo '(作者，编辑 )';
					}
					else {
						echo '(作者，会员)';
					}
				}
				else {
					echo '(成员)';
				}
			?>
			<span class="user-vote">
			  <?php
				if(disnametoid(get_comment_author()) == NULL) {
					echo '';
				}
				else {
				 echo '评分：'.get_user_vote(disnametoid(get_comment_author()), get_the_ID()); 
				}
			  ?>
			</span>
		</div>
		<div class="comment-content-text">
			<?php if ($comment->comment_approved == '0') : ?>   
				<em>你的评论正在审核，稍后会显示出来！</em><br />   
			<?php endif; ?>   
			<?php comment_text(); ?>			
		</div>
	  </div>
		<div class="comment-content-info right">
			<span class="comment-content-date"><?php echo get_comment_time('Y-m-d H:i'); ?></span>
			<span class="comment-content-edit">
				<?php
					global $current_user;
					get_currentuserinfo();
					if( $current_user->display_name == get_comment_author_link() || current_user_can('level_10') ) {
						edit_comment_link('编辑');
					}
				?>
			</span>
			<span class="comment-content-reply">
				<?php if ($depth == get_option('thread_comments_depth')) : ?>
					 <a onclick="return addComment.moveForm( 'comment-<?php comment_ID() ?>','<?php echo $comment->comment_parent; ?>', 'respond','<?php echo $comment->comment_post_ID; ?>' )" href="?replytocom=<?php comment_ID() ?>#respond" class="comment-reply-link" rel="nofollow">回复</a>
				 <?php else: ?>
					 <a onclick="return addComment.moveForm( 'comment-<?php comment_ID() ?>','<?php comment_ID() ?>', 'respond','<?php echo $comment->comment_post_ID; ?>' ) " href="?replytocom=<?php comment_ID() ?>#respond" class="comment-reply-link" rel="nofollow">回复</a>
				 <?php endif; ?>				
			</span>
			<span class="comment-content-delete">
				<?php   
				if ( $current_user->user_ID == get_post()->post_author || current_user_can('level_10') ) {   
				$url = get_bloginfo('url');   
				echo '<a id="delete-'. $comment->comment_ID .'" href="' . wp_nonce_url("$url/wp-admin/comment.php?action=deletecomment&p=" . $comment->comment_post_ID . '&c=' . $comment->comment_ID, 'delete-comment_' . $comment->comment_ID) . '"" >删除</a>';   
				}   
				?>
			</span>
		</div>
	</div>
	<div class="clear"></div>
    <?php die();
}
function ajax_comment_err($a) {
    header('HTTP/1.0 500 Internal Server Error');
    header('Content-Type: text/plain;charset=UTF-8');
    echo $a;
    exit;
}